package com.sunhao.myapplication.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.sunhao.myapplication.entity.GoodsInfo;

import java.util.List;

public class ShoppingDBHelper extends SQLiteOpenHelper {
    //数据库名字,表名,数据库版本,helper,写连接,读连接


    //数据库名
    private static final String DB_NAME = "shopping.db";

    //商品信息表
    private static final String TABLE_GOODS_INFO = "goods_info";

    //购物车信息表
    private static final String TABLE_CART_INFO = "cart_info";


    private static final Integer DB_VERSION = 1;
    private static ShoppingDBHelper helper = null;
    private SQLiteDatabase WDB = null;
    private SQLiteDatabase RDB = null;

    //私有构造器
    private ShoppingDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    //利用单例模式获取数据库帮助器的唯一的实例
    //确保只有一个帮助器实例
    public static ShoppingDBHelper getInstance(Context context) {
        if (helper == null) {
            helper = new ShoppingDBHelper(context);
        }
        return helper;
    }


    //数据库读连接
    public SQLiteDatabase openReadLink() {
        if (RDB == null || !RDB.isOpen()) {
            RDB = helper.getReadableDatabase();
        }
        return RDB;
    }

    //数据库写连接
    public SQLiteDatabase openWriteLink() {
        if (WDB == null || !WDB.isOpen()) {
            WDB = helper.getWritableDatabase();
        }
        return WDB;
    }

    //数据库关闭连接
    //如果读写连接开着,就关闭
    public void closeLink() {
        if (RDB != null || RDB.isOpen()) {
            RDB.close();
            RDB = null;
        }
        if (WDB != null || WDB.isOpen()) {
            WDB.close();
            WDB = null;
        }
    }


    //创建数据库
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //创建商品信息表
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_GOODS_INFO
                + " (_id integer PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "  name varchar not NULL," +
                "  description varchar not NULL," +
                "  price float  not NULL," +
                "  pic_path varchar  not NULL" +
                ");";
        sqLiteDatabase.execSQL(sql);

        //创建购物车信息表
        sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CART_INFO
                + " (_id integer PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "  goods_id Integer not NULL," +
                "  count INTEGER not NULL" +
                ");";
        sqLiteDatabase.execSQL(sql);

    }


    //数据库版本发生变化时触发
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    //新增数据
    public void insertGoodsInfo(List<GoodsInfo> list) {
        //插入多条记录,要么全部成功,要么全部失败
        try {
            WDB.beginTransaction();
//          遍历商品信息
            for (GoodsInfo info : list) {
                ContentValues values = new ContentValues();
                values.put("name", info.name);
                values.put("description", info.description);
                values.put("price", info.price);
                values.put("pic_path", info.picPath);
                WDB.insert(TABLE_GOODS_INFO, null, values);
            }
            WDB.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            WDB.endTransaction();
        }
    }


}
